package com.tlgen.coopera.config.disconf;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RefreshScope implements Scope {

    private static final String TARGET = "scopedTarget.";

    private static final Map<String, Object> map = new ConcurrentHashMap<>();

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Object bean = map.get(name);
        // 加个缓存判断
//        return bean == null ? map.put(name, objectFactory.getObject()) : bean;
        return objectFactory.getObject();
    }

    // 自定义一个缓存失效方法，很奇怪的是这个地方的beanName是方法名a
    public void refresh(String name) {
        map.remove(TARGET + name);
    }

    @Override
    public Object remove(String name) {
        return null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {

    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }

}